﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for MessageDB
/// </summary>
/// 
namespace CE.User
{
    public class MessageDB
    {

        public Int64 Insert(Message message)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocUserMessageInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@MessageId", message.MessageId);
            _sPara.Add("@UserTypeId", message.UserTypeId);
            _sPara.Add("@MessageType", message.MessageType);
            _sPara.Add("@ErrorCode", message.ErrorCode);
            _sPara.Add("@Tittle", message.Tittle);
            _sPara.Add("@Header", message.Header);
            _sPara.Add("@Body", message.Body);
            _sPara.Add("@Footer", message.Footer);
            _sPara.Add("@Status", message.Status);
            _sPara.Add("@RefCEUserId", message.RefCEUserId);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public DataTable GetMessage(Int32 UserTypeId, string MessageType, Int32 ErrorCode)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserMessagesSelectItem";

            SortedList _spara = new SortedList();
            _spara.Add("@UserTypeId", UserTypeId);
            _spara.Add("@MessageType", MessageType);
            _spara.Add("@ErrorCode", ErrorCode);

            DataTable _dt = _db.GetDataTableBySP(_spName, _spara);
 
            return _dt;
        }

        public Message GetMessage(Int64 MessageId)
        {
            Message _message = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserMessagesSelectItemById";

            SortedList _spara = new SortedList();
            _spara.Add("@MessageId", MessageId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _message = new Message();
                    _message = FillDataRecord(_dt, 0);
                }
            }
            return _message;
        }

        private Message FillDataRecord(DataTable myDataTable, int RowId)
        {
            Message myMessage = new Message();
            myMessage.MessageId = Convert.ToInt64(myDataTable.Rows[RowId]["MessageId"].ToString());
            myMessage.UserTypeId = Convert.ToInt32(myDataTable.Rows[RowId]["UserTypeId"].ToString());
            myMessage.MessageType = myDataTable.Rows[RowId]["MessageType"].ToString();
            myMessage.ErrorCode = myDataTable.Rows[RowId]["ErrorCode"].ToString();
            myMessage.Tittle = myDataTable.Rows[RowId]["Tittle"].ToString();
            myMessage.Header = myDataTable.Rows[RowId]["Header"].ToString();
            myMessage.Body = myDataTable.Rows[RowId]["Body"].ToString();
            myMessage.Footer = myDataTable.Rows[RowId]["Footer"].ToString();
            myMessage.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            return myMessage;
        }


    }
}